package com.example.study_manger.control;

import com.example.study_manger.entity.ExamQuestion;
import com.example.study_manger.pojo.PageExamQuestion;
import com.example.study_manger.service.ExamQuestionService;
import com.example.study_manger.tools.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/examQuestions")
public class ExamQuestionController {

    @Autowired
    private ExamQuestionService examQuestionService;

    // 获取试题列表（支持分页和条件查询）
    @GetMapping
    public R<PageExamQuestion> getExamQuestions(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String questionType) {
        Page<ExamQuestion> examQuestions = examQuestionService.getExamQuestions(page, pageSize, questionType);
        List<ExamQuestion> content = examQuestions.getContent();
        Long totalElements = examQuestions.getTotalElements();
        return R.success(new PageExamQuestion(totalElements,content));
    }

    // 添加试题
    @PostMapping
    public R<ExamQuestion> addExamQuestion(@RequestBody ExamQuestion examQuestion) {
        ExamQuestion savedExamQuestion = examQuestionService.addExamQuestion(examQuestion);
        return R.success(savedExamQuestion);
    }

    // 编辑试题
    @PutMapping("/{id}")
    public R<ExamQuestion> editExamQuestion(@PathVariable Long id, @RequestBody ExamQuestion examQuestion) {
        ExamQuestion updatedExamQuestion = examQuestionService.editExamQuestion(id, examQuestion);
        return R.success(updatedExamQuestion);
    }

    // 删除试题
    @DeleteMapping("/{id}")
    public R<String> deleteExamQuestion(@PathVariable Long id) {
        String b = examQuestionService.deleteExamQuestion(id);
        if (b.equals("true")) {
            return R.success(b);
        }
        return R.fail(b);
    }
}

